package com.iweb.commodity.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iweb.commodity.model.Commodity;
import com.iweb.commodity.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jack
 * @since 2020-05-10
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    // 查看所有
    @GetMapping
    public ResponseEntity<List<Commodity>> selectList() {
        List<Commodity> commodities = commodityService.list();
        return ResponseEntity.ok(commodities);
    }

    // 插入商品
    @PostMapping
    public ResponseEntity<Commodity> addCommodity(@RequestBody Commodity commodity) {
        commodity.setCommoCount(0);
        commodityService.save(commodity);
        return ResponseEntity.ok(commodity);
    }

    // 商品名称模糊查询
    @PostMapping("selectListLikeName")
    public ResponseEntity<List<Commodity>> selectListLikeName(@RequestBody Commodity commodity) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("commo_name", commodity.getCommoName());
        List<Commodity> commodities = commodityService.list(queryWrapper);
        return ResponseEntity.ok(commodities);
    }
}

